package com.goktech.controller;

import com.goktech.entity.Book;
import com.goktech.entity.User;
import com.goktech.entity.borrow;
import com.goktech.service.BookService;
import com.goktech.service.BorrowService;
import com.goktech.service.UserService;
import com.goktech.service.imp.bookServiceImp;
import com.goktech.service.imp.borrowServiceImp;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName userBorrowController
 * @Description TODO
 * @Author 邱华英
 * @Date 2022/10/9 20:10
 * @Version 1.0
 */

@Controller
@RequestMapping("/user/borrow")
public class userBorrowController {

    @Autowired
    private BorrowService borrowService;

    @Autowired
    private borrow b;

    @Autowired
    private Book book;

    @Autowired
    private BookService bookService;

    @Autowired
    private User user;

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String borrowSelectAll(){
        return "redirect:search/add?yeshu=0";
    }

    @RequestMapping("/search/add")
    public String searchAdd(Integer yeshu, HttpServletRequest request){
        List<Map<String, Object>> byAll = null;

        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        user.setZhanghao(zhanghao);
        User byZhanghao = userService.findByZhanghao(user);
        byAll = borrowService.findByUserId(byZhanghao,yeshu * 5, 5);

        if(byAll.size() == 0 && yeshu!=0){
            byAll = borrowService.findByUserId(byZhanghao,yeshu * 5, 5);
        }else{
            yeshu=yeshu+1;
        }
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("borrows",byAll);
        return "user/personBorrows";
    }

    @RequestMapping("/search/delete")
    public String searchDelete(Integer yeshu,HttpServletRequest request){
        List<Map<String, Object>> byAll = null;
        if(yeshu>1){
            yeshu=yeshu-1;
        }
        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        user.setZhanghao(zhanghao);
        User byZhanghao = userService.findByZhanghao(user);
        byAll = borrowService.findByUserId(byZhanghao,(yeshu-1)*5,5);
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("borrows",byAll);
        return "user/personBorrows";
    }

    @PostMapping("/return")
    @ResponseBody
    public boolean returnBook(@Param("borrid")Integer borrid,@Param("bookid")Integer bookid){
        b.setId(borrid);
        borrow byBorrow = borrowService.findByBorrow(b);
        System.out.println(byBorrow);
        String isReturn = byBorrow.getIsReturn();
        if(isReturn!=null && isReturn.equals("true")){
            return false;
        }
        byBorrow.setIsReturn("true");

        byBorrow.setReallyTime(new Date());

        Date endTime = byBorrow.getReturnTime();
        Date xujieTime = byBorrow.getXujieTime();

        if(xujieTime != null) {
            endTime = xujieTime;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date d =new Date();
        String format1 = simpleDateFormat.format(d);
        String format = simpleDateFormat.format(endTime);
        String[] split1 = format1.split("-");
        String[] split2 = format.split("-");
        LocalDate startDate = LocalDate.of(Integer.parseInt(split1[0]),Integer.parseInt(split1[1]),Integer.parseInt(split1[2]));
        LocalDate startDate1 = LocalDate.of(Integer.parseInt(split2[0]),Integer.parseInt(split2[1]),Integer.parseInt(split2[2]));
        long daysDiff = ChronoUnit.DAYS.between(startDate1, startDate);
        System.out.println("daysDiff:"+daysDiff);
        if(daysDiff>0){
            byBorrow.setOverTime(daysDiff);
        }
        int i = borrowService.updateBorrow1(byBorrow);
        book.setId(bookid);
        Book byBook = bookService.findByBook(book);
        byBook.setYuliang(byBook.getYuliang()+1);
        int i1 = bookService.updateBook(byBook);
        if(i > 0 && i1 > 0){
            return true;
        }
        return false;
    }

    @PostMapping("/xujie")
    @ResponseBody
    public boolean xujieTime(@Param("borrid")Integer borrid,@Param("bookid")Integer bookid,@Param("xujieTime")String xujieTime){
        b.setId(borrid);
        Map<String, Object> byBorrid = borrowService.findByBorrid(b);
        Date xujieTime1 = (Date) byBorrid.get("xujieTime");
        String aReturn = (String) byBorrid.get("isreturn");

        if((aReturn != null)&& (xujieTime1 != null || aReturn.equals("true"))){
            return false;
        }else{
            borrow byBorrow = borrowService.findByBorrow(b);
            byBorrow.setId(borrid);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date d =new Date();
            String format = simpleDateFormat.format(d);
            String[] split1 = format.split("-");
            String[] split2 = xujieTime.split("-");
            boolean flag = false;
            for(int i = 0;i < split1.length;i++){
                int i1 = Integer.parseInt(split1[i]) ;
                int i2 = Integer.parseInt(split2[i]);
                if(i2 > i1){
                    flag = true;
                    break;
                }
            }


            if(flag){
                try {
                    Date returnTime1 = simpleDateFormat.parse(xujieTime);
                    byBorrow.setXujieTime(returnTime1);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int i = borrowService.updateBorrow(byBorrow);
                if(i>0){
                    return true;
                }
            }
        }
        return false;
    }
}
